class LibraryDocument < ActiveRecord::Base

  attr_accessible :author, :cover_image, :category_id, :edited_on, :in_store_on, :created_on, :isbn, :language, :lend_count, :published_on, :quantity, :revision, :status, :summary, :tags, :time_limit, :title, :view_count
  before_save :init_data
  #after_destroy :delete_all_borrow_records
  #after_find :do_format_status

  #relation declare
  has_many :library_borrow_records

  #private
  #def delete_all_borrow_records
  #  LibraryBorrowRecord.destroy_all(:library_document_id => self.id)
  #end

  private
  def init_data
    self.time_limit = self.time_limit.nil? ? 10: self.time_limit
    #self.in_store_on = self.in_store_on.nil? ? Time.now : self.in_store_on
    self.status = self.status.nil? || self.status.blank? ? '1' : self.status #default: accessible
    self.quantity = self.quantity.nil? ? 1 : self.quantity  #default: 1 copy
    self.view_count = self.view_count.nil? ? 0 : self.view_count  #default: 1 copy
    self.lend_count = self.lend_count.nil? ? 1 : self.lend_count  #default: 1 copy
    self.edited_on = Time.now
  end

  #def do_format_status
  #  status_array = {
  #      '0' => I18n.t('library.document.not_available'),
  #      '1' => I18n.t('library.document.available'),
  #      '-1' => I18n.t('library.document.deleted'),
  #      ''  =>  I18n.t('library.document.unknown')
  #  }
  #  self.status = status_array[self.status]
  #end

end
